package com.github.postsanf.yinian.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/4/6.
 */

public class MyCustomViewBehind extends ViewGroup {
    private static final String TAG = "CustomViewBehind";
    private static final int MARGIN_THRESHOLD = 48;
    private int mTouchMode;
    private MyCustomViewAbove mViewAbove;
    private View mContent;
    private View mSecondaryContent;
    private int mMarginThreshold;
    private int mWidthOffset;
    private MySlidingMenu.CanvasTransformer mTransformer;
    private boolean mChildrenEnabled;
    private int mMode;
    private boolean mFadeEnabled;
    private final Paint mFadePaint;
    private float mScrollScale;
    private Drawable mShadowDrawable;
    private Drawable mSecondaryShadowDrawable;
    private int mShadowWidth;
    private float mFadeDegree;
    private boolean mSelectorEnabled;
    private Bitmap mSelectorDrawable;
    private View mSelectedView;

    public MyCustomViewBehind(Context context) {
        this(context, (AttributeSet) null);
    }

    public MyCustomViewBehind(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mTouchMode = 0;
        this.mFadePaint = new Paint();
        this.mSelectorEnabled = true;
        this.mMarginThreshold = (int) TypedValue.applyDimension(1, 48.0F, this.getResources().getDisplayMetrics());
    }

    public void setCustomViewAbove(MyCustomViewAbove customViewAbove) {
        this.mViewAbove = customViewAbove;
    }

    private float mOffsetFadeDegree;

    public void setOffsetFadeDegree(float degree) {
        if (degree > 1.0f || degree < 0.0f)
            throw new IllegalStateException("The offsetFadeDegree must be between 0.0f and 1.0f");
        mOffsetFadeDegree = degree;
    }

    /**
     * 绘制剩余View的淡出淡入
     *
     * @param content     主界面
     * @param canvas      画布
     * @param openPercent 打开了的百分比
     */
    public void drawOffsetFade(View content, Canvas canvas, float openPercent) {
        if (mWidthOffset <= 0 || mOffsetFadeDegree <= 0) return;   //不开启就返回，不设置 淡入淡出效果

        final int alpha = (int) (mOffsetFadeDegree * 255 * Math.abs(openPercent));
        mFadePaint.setColor(Color.argb(alpha, 0, 0, 0));
        canvas.drawRect(content.getLeft(), 0, content.getRight(), getHeight(), mFadePaint);
    }

    public void setCanvasTransformer(MySlidingMenu.CanvasTransformer t) {
        this.mTransformer = t;
    }

    public void setWidthOffset(int i) {
        this.mWidthOffset = i;
        this.requestLayout();
    }

    public int getBehindWidth() {
        return this.mContent.getWidth();
    }

    public void setContent(View v) {
        if (this.mContent != null) {
            this.removeView(this.mContent);
        }

        this.mContent = v;
        this.addView(this.mContent);
    }

    public View getContent() {
        return this.mContent;
    }

    public void setSecondaryContent(View v) {
        if (this.mSecondaryContent != null) {
            this.removeView(this.mSecondaryContent);
        }

        this.mSecondaryContent = v;
        this.addView(this.mSecondaryContent);
    }

    public View getSecondaryContent() {
        return this.mSecondaryContent;
    }

    public void setChildrenEnabled(boolean enabled) {
        this.mChildrenEnabled = enabled;
    }

    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
        if (this.mTransformer != null) {
            this.invalidate();
        }

    }

    public boolean onInterceptTouchEvent(MotionEvent e) {
        return !this.mChildrenEnabled;
    }

    public boolean onTouchEvent(MotionEvent e) {
        return !this.mChildrenEnabled;
    }

    protected void dispatchDraw(Canvas canvas) {
        if (this.mTransformer != null) {
            canvas.save();
            this.mTransformer.transformCanvas(canvas, this.mViewAbove.getPercentOpen());
            super.dispatchDraw(canvas);
            canvas.restore();
        } else {
            super.dispatchDraw(canvas);
        }

    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r - l;
        int height = b - t;
        this.mContent.layout(0, 0, width - this.mWidthOffset, height);
        if (this.mSecondaryContent != null) {
            this.mSecondaryContent.layout(0, 0, width - this.mWidthOffset, height);
        }

    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(0, widthMeasureSpec);
        int height = getDefaultSize(0, heightMeasureSpec);
        this.setMeasuredDimension(width, height);
        int contentWidth = getChildMeasureSpec(widthMeasureSpec, 0, width - this.mWidthOffset);
        int contentHeight = getChildMeasureSpec(heightMeasureSpec, 0, height);
        this.mContent.measure(contentWidth, contentHeight);
        if (this.mSecondaryContent != null) {
            this.mSecondaryContent.measure(contentWidth, contentHeight);
        }

    }

    public void setMode(int mode) {
        if (mode == 0 || mode == 1) {
            if (this.mContent != null) {
                this.mContent.setVisibility(View.VISIBLE);
            }

            if (this.mSecondaryContent != null) {
                this.mSecondaryContent.setVisibility(View.INVISIBLE);
            }
        }

        this.mMode = mode;
    }

    public int getMode() {
        return this.mMode;
    }

    public void setScrollScale(float scrollScale) {
        this.mScrollScale = scrollScale;
    }

    public float getScrollScale() {
        return this.mScrollScale;
    }

    public void setShadowDrawable(Drawable shadow) {
        this.mShadowDrawable = shadow;
        this.invalidate();
    }

    public void setSecondaryShadowDrawable(Drawable shadow) {
        this.mSecondaryShadowDrawable = shadow;
        this.invalidate();
    }

    public void setShadowWidth(int width) {
        this.mShadowWidth = width;
        this.invalidate();
    }

    public void setFadeEnabled(boolean b) {
        this.mFadeEnabled = b;
    }

    public void setFadeDegree(float degree) {
        if (degree <= 1.0F && degree >= 0.0F) {
            this.mFadeDegree = degree;
        } else {
            throw new IllegalStateException("The BehindFadeDegree must be between 0.0f and 1.0f");
        }
    }

    public int getMenuPage(int page) {
        page = page > 1 ? 2 : (page < 1 ? 0 : page);
        return this.mMode == 0 && page > 1 ? 0 : (this.mMode == 1 && page < 1 ? 2 : page);
    }

    public void scrollBehindTo(View content, int x, int y) {
        int vis = View.VISIBLE;
        if (this.mMode == 0) {
            if (x >= content.getLeft()) {
                vis = View.INVISIBLE;
            }
            this.scrollTo((int) ((float) (x + this.getBehindWidth()) * this.mScrollScale), y);
        } else if (this.mMode == 1) {
            if (x <= content.getLeft()) {
                vis = View.INVISIBLE;
            }
            this.scrollTo((int) ((float) (this.getBehindWidth() - this.getWidth()) + (float) (x - this.getBehindWidth()) * this.mScrollScale), y);
        } else if (this.mMode == 2) {
            this.mContent.setVisibility(x >= content.getLeft() ? View.INVISIBLE : View.VISIBLE);
            this.mSecondaryContent.setVisibility(x <= content.getLeft() ? View.INVISIBLE : View.VISIBLE);
            vis = x == View.VISIBLE ? View.INVISIBLE : View.VISIBLE;
            if (x <= content.getLeft()) {
                this.scrollTo((int) ((float) (x + this.getBehindWidth()) * this.mScrollScale), y);
            } else {
                this.scrollTo((int) ((float) (this.getBehindWidth() - this.getWidth()) + (float) (x - this.getBehindWidth()) * this.mScrollScale), y);
            }
        }
        if (vis == View.INVISIBLE) {
            Log.v("CustomViewBehind", "behind INVISIBLE");
        }
        this.setVisibility(vis);
    }

    public int getMenuLeft(View content, int page) {
        if (this.mMode == 0) {
            switch (page) {
                case 0:
                    return content.getLeft() - this.getBehindWidth();
                case 1:
                default:
                    break;
                case 2:
                    return content.getLeft();
            }
        } else if (this.mMode == 1) {
            switch (page) {
                case 0:
                    return content.getLeft();
                case 1:
                default:
                    break;
                case 2:
                    return content.getLeft() + this.getBehindWidth();
            }
        } else if (this.mMode == 2) {
            switch (page) {
                case 0:
                    return content.getLeft() - this.getBehindWidth();
                case 1:
                default:
                    break;
                case 2:
                    return content.getLeft() + this.getBehindWidth();
            }
        }

        return content.getLeft();
    }

    public int getAbsLeftBound(View content) {
        return this.mMode != 0 && this.mMode != 2 ? (this.mMode == 1 ? content.getLeft() : 0) : content.getLeft() - this.getBehindWidth();
    }

    public int getAbsRightBound(View content) {
        return this.mMode == 0 ? content.getLeft() : (this.mMode != 1 && this.mMode != 2 ? 0 : content.getLeft() + this.getBehindWidth());
    }

    public boolean marginTouchAllowed(View content, int x) {
        int left = content.getLeft();
        int right = content.getRight();
        return this.mMode == 0 ? x >= left && x <= this.mMarginThreshold + left : (this.mMode == 1 ? x <= right && x >= right - this.mMarginThreshold : (this.mMode != 2 ? false : x >= left && x <= this.mMarginThreshold + left || x <= right && x >= right - this.mMarginThreshold));
    }

    public void setTouchMode(int i) {
        this.mTouchMode = i;
    }

    public boolean menuOpenTouchAllowed(View content, int currPage, float x) {
        switch (this.mTouchMode) {
            case 0:
                return this.menuTouchInQuickReturn(content, currPage, x);
            case 1:
                return true;
            default:
                return false;
        }
    }

    public boolean menuTouchInQuickReturn(View content, int currPage, float x) {
        return this.mMode != 0 && (this.mMode != 2 || currPage != 0) ? (this.mMode != 1 && (this.mMode != 2 || currPage != 2) ? false : x <= (float) content.getRight()) : x >= (float) content.getLeft();
    }

    public boolean menuClosedSlideAllowed(float dx) {
        return this.mMode == 0 ? dx > 0.0F : (this.mMode == 1 ? dx < 0.0F : this.mMode == 2);
    }

    public boolean menuOpenSlideAllowed(float dx) {
        return this.mMode == 0 ? dx < 0.0F : (this.mMode == 1 ? dx > 0.0F : this.mMode == 2);
    }

    public void drawShadow(View content, Canvas canvas) {
        if (this.mShadowDrawable != null && this.mShadowWidth > 0) {
            int left = 0;
            if (this.mMode == 0) {
                left = content.getLeft() - this.mShadowWidth;
            } else if (this.mMode == 1) {
                left = content.getRight();
            } else if (this.mMode == 2) {
                if (this.mSecondaryShadowDrawable != null) {
                    left = content.getRight();
                    this.mSecondaryShadowDrawable.setBounds(left, 0, left + this.mShadowWidth, this.getHeight());
                    this.mSecondaryShadowDrawable.draw(canvas);
                }

                left = content.getLeft() - this.mShadowWidth;
            }

            this.mShadowDrawable.setBounds(left, 0, left + this.mShadowWidth, this.getHeight());
            this.mShadowDrawable.draw(canvas);
        }
    }

    public void drawFade(View content, Canvas canvas, float openPercent) {
        if (this.mFadeEnabled) {
            int alpha = (int) (this.mFadeDegree * 255.0F * Math.abs(1.0F - openPercent));
            this.mFadePaint.setColor(Color.argb(alpha, 0, 0, 0));
            int left = 0;
            int right = 0;
            if (this.mMode == 0) {
                left = content.getLeft() - this.getBehindWidth();
                right = content.getLeft();
            } else if (this.mMode == 1) {
                left = content.getRight();
                right = content.getRight() + this.getBehindWidth();
            } else if (this.mMode == 2) {
                left = content.getLeft() - this.getBehindWidth();
                right = content.getLeft();
                canvas.drawRect((float) left, 0.0F, (float) right, (float) this.getHeight(), this.mFadePaint);
                left = content.getRight();
                right = content.getRight() + this.getBehindWidth();
            }

            canvas.drawRect((float) left, 0.0F, (float) right, (float) this.getHeight(), this.mFadePaint);
        }
    }

    public void drawSelector(View content, Canvas canvas, float openPercent) {
        if (this.mSelectorEnabled) {
            if (this.mSelectorDrawable != null && this.mSelectedView != null) {
                String tag = (String) this.mSelectedView.getTag(com.slidingmenu.lib.R.id.selected_view);
                if (tag.equals("CustomViewBehindSelectedView")) {
                    canvas.save();
                    int offset = (int) ((float) this.mSelectorDrawable.getWidth() * openPercent);
                    int left;
                    int right;
                    if (this.mMode == 0) {
                        right = content.getLeft();
                        left = right - offset;
                        canvas.clipRect(left, 0, right, this.getHeight());
                        canvas.drawBitmap(this.mSelectorDrawable, (float) left, (float) this.getSelectorTop(), (Paint) null);
                    } else if (this.mMode == 1) {
                        left = content.getRight();
                        right = left + offset;
                        canvas.clipRect(left, 0, right, this.getHeight());
                        canvas.drawBitmap(this.mSelectorDrawable, (float) (right - this.mSelectorDrawable.getWidth()), (float) this.getSelectorTop(), (Paint) null);
                    }

                    canvas.restore();
                }
            }

        }
    }

    public void setSelectorEnabled(boolean b) {
        this.mSelectorEnabled = b;
    }

    public void setSelectedView(View v) {
        if (this.mSelectedView != null) {
            this.mSelectedView.setTag(com.slidingmenu.lib.R.id.selected_view, (Object) null);
            this.mSelectedView = null;
        }

        if (v != null && v.getParent() != null) {
            this.mSelectedView = v;
            this.mSelectedView.setTag(com.slidingmenu.lib.R.id.selected_view, "CustomViewBehindSelectedView");
            this.invalidate();
        }

    }

    private int getSelectorTop() {
        int y = this.mSelectedView.getTop();
        y += (this.mSelectedView.getHeight() - this.mSelectorDrawable.getHeight()) / 2;
        return y;
    }

    public void setSelectorBitmap(Bitmap b) {
        this.mSelectorDrawable = b;
        this.refreshDrawableState();
    }
}